define(
    function (require) {
        require('tpl!./list.tpl.html');

        var util = require('common/util');

        var globalData = require('common/GlobalData').getInstance();

        var mallList = [];
        var mallTree = globalData.getMallList(true);
        util.walkTree(mallTree, function (item) {if (item.type === 'mall') {mallList.push(item);}});

        var showid = 1;
        
        var fields = [
            {
                title: '编号',
                field: 'id' ,
                sortable: true,
                resizable: true,
                width: 40,
                content: function (item) {
                    return showid++;
                }
            },
            {
                title: '商户名称',
                field: 'companyName' ,
                resizable : true,
                width: 150,
                content: function (item) {
                    return '<span class="list-link" data-command-args="' + item.id + '" data-command="modify">' + item.companyName + '</a>';
                }
            },
            {
                title: '商场',
                field: 'cityname' ,
                sortable: true,
                resizable : true,
                width: 120,
                content: function (item) {
                    var mall = util.find(mallList, function (mall) {return mall.value == 'mall_' + item.mallid})
                    if (!util.isEmpty(mall)) {
                        return mall.text;
                    }
                    return '未知';
                }
            },
            {
                title: '活动开始时间',
                field: 'startTime' ,
                sortable: true,
                resizable : true,
                width: 150,
                content: function (item) {
                    return item.startTime;
                }
            },
            {
                title: '活动结束时间',
                field: 'endTime' ,
                sortable: true,
                resizable : true,
                width: 150,
                content: function (item) {
                    return item.endTime;
                }
            },
            {
                title: '管理员名称',
                field: 'chargePerson' ,
                sortable: true,
                resizable : true,
                width: 100,
                content: function (item) {
                     return item.chargePerson;
                }
            },
            {
                title: '管理员电话',
                field: 'phone' ,
                sortable: true,
                resizable : true,
                width: 100,
                content: function (item) {
                    if (item.phone !== '') {
                        return item.phone;
                    }
                    return '--'
                }
            },
            {
                title: '品牌LOGO',
                field: 'logoPic' ,
                sortable: true,
                resizable : true,
                width: 100,
                content: function (item) {
                    if (item.logoPic !== '' && item.logoPic.length > 5 && item.logoPic.substring(0, 5) === 'http:') {
                        return '<img src="' + item.logoPic + '" style="width:50px;height:40px;">';
                    } else {
                        return item.logoPic;
                    }
                }
            },
            {
                title: '登录信息(用户名 / 密码)',
                field: 'login' ,
                sortable: true,
                resizable : true,
                width: 150,
                content: function (item) {
                    return item.username + ' / ' + item.password;
                }
            },
            {
                title: '操作',
                field: 'operate' ,
                width: 60,
                content: function (item) {
                    return '<span class="list-link" data-command-args="' + item.id + '" data-command="delete">删除</a>';
                }
            }
        ];
        
        var BaseListView = require('common/ListView');

        function SurpriseShopListView() {
            BaseListView.apply(this, arguments);
        }

        SurpriseShopListView.prototype.template = 'surpriseShopListPage';

        SurpriseShopListView.prototype.getUIProperties = function () {
            BaseListView.prototype.getUIProperties.apply(this, arguments);
            showid = 1;
            return {
                list: {
                    fields: fields,
                    sortable: false,
                    columnResizable: true,
                    subrow: false,
                    followHead: true,
                    selectMode: 'line',
                    noDataHtml: '<p style="margin:0;">暂无商户信息!</p>'
                }
            };
        }

        SurpriseShopListView.prototype.enterDocument = function () {
            BaseListView.prototype.enterDocument.apply(this, arguments);

            var me = this;
            this.get('list').on(
                'command',
                function (event) {
                    me.fire(event.name, event);
                }
            );

            this.get('createButton').on(
                'click',
                this.fire.bind(this, 'create')
            );
        }

        require('er/util').inherits(SurpriseShopListView, BaseListView);
        return SurpriseShopListView;
    }
);
